﻿define(['services/datacontext', 'services/helpers', 'durandal/plugins/router'],
    function (datacontext, helpers, router) {
        var cars = ko.observableArray(),
            latestCars = ko.observableArray();

    //#region Internal Methods
    function activate() {
        return Q.all([
            datacontext.getCarsBySize(cars, 'GetFeaturedCars', 10),
            datacontext.getCarsBySize(latestCars, 'GetNewCars', 3)
        ]);
    }
    //#endregion
        
    var viewAttached = function (view) {
        helpers.runCarousel();
        helpers.detectIDevice();
        helpers.bindToEventList(view, '.oneByOne_item', gotoDetails, 'click');
        helpers.bindToEventList(view, '.car-thumbnails', gotoDetails, 'click');
    };
        

    var gotoDetails = function (selectedCar) {
        if (selectedCar.id() !== null) {
            var url = '#/cardetails/' + selectedCar.id();
            router.navigateTo(url);
        }
    };
        
    var vm = {
        cars: cars,
        latestCars: latestCars,
        activate: activate,
        title: 'Home View',
        viewAttached: viewAttached
    };

    return vm;
});